package com.ziq.base.view.drawable

import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable

/**
 * 生成StateListDrawable
 * */
fun stateListDrawableBuild(
    block: StateListDrawableBuilder.()->Unit
): StateListDrawable {
    return StateListDrawableBuilder().also(block).build()
}

class StateListDrawableBuilder{

    private val drawableItems: MutableList<DrawableItem> = mutableListOf()

    fun build():StateListDrawable{
        return StateListDrawable().apply {
            for (item in drawableItems){
                addState(item.state, item.drawable)
            }
        }
    }

    /**
     * 注意添加的先后顺序，先添加的先判断状态，避免后面的状态没机会匹配
     * */
    fun drawableItem(block: DrawableItem.()->Unit): Unit {
        drawableItems.add(DrawableItem().also(block))
    }

    class DrawableItem{
        var drawable: Drawable? = null
        //android.R.attr.state_pressed = true
        //-android.R.attr.state_pressed = false // 前面加 "-"
        var state:IntArray = intArrayOf()
    }
}